package ru.mcfr.oxygen.framework.operations.table;

import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.access.AuthorTableAccess;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 09.03.11
 * Time: 14:26
 * To change this template use File | Settings | File Templates.
 */
public class DeleteInsertRowOperation implements AuthorOperation {
    public void doOperation(AuthorAccess authorAccess, ArgumentsMap argumentsMap) throws IllegalArgumentException, AuthorOperationException {
        AuthorDocumentController controller = authorAccess.getDocumentController();
        AuthorSchemaManager manager = authorAccess.getDocumentController().getAuthorSchemaManager();
        AuthorTableAccess tableContainer = authorAccess.getTableAccess();

        String selectedText = "";
        try {
            selectedText = authorAccess.getEditorAccess().getSelectedText();
        } catch (Exception e) {
        }

        try{
            int currentPosition = authorAccess.getEditorAccess().getCaretOffset();
            if (!selectedText.isEmpty())
                currentPosition = authorAccess.getEditorAccess().getSelectionStart();

            AuthorElement ae = (AuthorElement) controller.getNodeAtOffset(currentPosition);
            AuthorNode an = (AuthorNode) ae;
            while (!an.getName().equals("строка"))
                an = an.getParent();

            int position = authorAccess.getEditorAccess().getCaretOffset();
            if (argumentsMap.getArgumentValue("вид операции").equals("удалить")){
                controller.deleteNode(an);
                authorAccess.getEditorAccess().setCaretPosition(position);
                return;
            }

            int pointToInsert = an.getEndOffset() + 1;

            AuthorNode aTable = an;
            while (!aTable.getName().equals("таблица"))
                aTable = aTable.getParent();

            AuthorElement[] cols = ((AuthorElement) aTable).getElementsByLocalName("колонка");

            String xmlText = "<строка>";
            for (int i = 0; i < cols.length; i++){
                xmlText += "<ячейка><абзац/></ячейка>";
            }
            xmlText += "</строка>";

            AuthorDocumentFragment fragment = controller.createNewDocumentFragmentInContext(xmlText, pointToInsert);
            controller.insertFragment(pointToInsert,fragment);
            authorAccess.getEditorAccess().setCaretPosition(position);

            //controller.insertXMLFragment(xmlText ,pointToInsert);

        } catch (Exception e) {}
    }

    public ArgumentDescriptor[] getArguments() {
        return new ArgumentDescriptor[]{
                new ArgumentDescriptor("вид операции",ArgumentDescriptor.TYPE_STRING,"удалить/вставить"),
                new ArgumentDescriptor("направление",ArgumentDescriptor.TYPE_STRING,"перед/после"),
        };
    }

    public String getDescription() {
        return "Вставить строку после";
    }
}
